<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <script>
    // 1.防抖
    function debounce(fn, wait) {
      var timer;
      return function () {
        if (timer) clearTimeout(timer);
        timer = setTimeout(() => {
          fn.apply(this, arguments)
        }, wait)
      }
    }
    // 2. 防抖（优化立即执行
    function debounce(fn, wait, immediate) {
      var timer
      return function() {
        if (timer) clearTimeout(timer)
        if (immediate) {
          var callNow = !timer // 通过一个变量来记录是否立即执行
          timer = setTimeout(() => {
            timer = null
          }, wait)
          if (callNow) fn.apply(this, arguments)
        } else {
          timer = setTimeout(() => {
            fn.apply(this, arguments)
          }, wait)
        }
      }
    }
//-------------------------------------------------------------------------------------------
    // 练习区：
    
    
    // 2.节流
    function throttle(fn, wait) {
      var canRun = true;
      return function () {
        if (!canRun) return;
        canRun = false;
        setTimeout(() => {
          fn.apply(this, arguments);
          canRun = true;
        }, wait)
      }
    }
//-------------------------------------------------------------------------------------------
    // 练习区：


  </script>
</body>
</html>